package cn.ds.assets.module.assets.controller.admin.project;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.ds.assets.framework.common.pojo.PageResult;
import cn.ds.assets.framework.common.pojo.CommonResult;
import static cn.ds.assets.framework.common.pojo.CommonResult.success;

import cn.ds.assets.framework.excel.core.util.ExcelUtils;

import cn.ds.assets.framework.operatelog.core.annotations.OperateLog;
import static cn.ds.assets.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.ds.assets.module.assets.controller.admin.project.vo.*;
import cn.ds.assets.module.assets.dal.dataobject.project.BaseProjectDO;
import cn.ds.assets.module.assets.convert.project.BaseProjectConvert;
import cn.ds.assets.module.assets.service.project.BaseProjectService;

//@Tag(name = "管理后台 - 项目")
//@RestController
//@RequestMapping("/assets/base-project")
@Validated
public class BaseProjectController {

    @Resource
    private BaseProjectService baseProjectService;

    @PostMapping("/create")
    @Operation(summary = "创建项目")
    @PreAuthorize("@ss.hasPermission('assets:base-project:create')")
    public CommonResult<Long> createBaseProject(@Valid @RequestBody BaseProjectCreateReqVO createReqVO) {
        return success(baseProjectService.createBaseProject(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新项目")
    @PreAuthorize("@ss.hasPermission('assets:base-project:update')")
    public CommonResult<Boolean> updateBaseProject(@Valid @RequestBody BaseProjectUpdateReqVO updateReqVO) {
        baseProjectService.updateBaseProject(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除项目")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('assets:base-project:delete')")
    public CommonResult<Boolean> deleteBaseProject(@RequestParam("id") Long id) {
        baseProjectService.deleteBaseProject(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得项目")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('assets:base-project:query')")
    public CommonResult<BaseProjectRespVO> getBaseProject(@RequestParam("id") Long id) {
        BaseProjectDO baseProject = baseProjectService.getBaseProject(id);
        return success(BaseProjectConvert.INSTANCE.convert(baseProject));
    }

    @GetMapping("/list")
    @Operation(summary = "获得项目列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('assets:base-project:query')")
    public CommonResult<List<BaseProjectRespVO>> getBaseProjectList(@RequestParam("ids") Collection<Long> ids) {
        List<BaseProjectDO> list = baseProjectService.getBaseProjectList(ids);
        return success(BaseProjectConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得项目分页")
    @PreAuthorize("@ss.hasPermission('assets:base-project:query')")
    public CommonResult<PageResult<BaseProjectRespVO>> getBaseProjectPage(@Valid BaseProjectPageReqVO pageVO) {
        PageResult<BaseProjectDO> pageResult = baseProjectService.getBaseProjectPage(pageVO);
        return success(BaseProjectConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出项目 Excel")
    @PreAuthorize("@ss.hasPermission('assets:base-project:export')")
    @OperateLog(type = EXPORT)
    public void exportBaseProjectExcel(@Valid BaseProjectExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BaseProjectDO> list = baseProjectService.getBaseProjectList(exportReqVO);
        // 导出 Excel
        List<BaseProjectExcelVO> datas = BaseProjectConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "项目.xls", "数据", BaseProjectExcelVO.class, datas);
    }

}
